<template>
  <div class="container">
    <el-card>
      <!-- logo -->
      <img class="logo" src="../../assets/logo.png" />
      <!-- 表单 -->
      <el-form class="login-form" :model="loginForm" :rules="loginRules" ref="loginForm">
        <!-- 手机号 -->
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号" v-model="loginForm.mobile" maxlength="11"></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="loginForm.code" maxlength="6"></el-input>
        </el-form-item>
        <!-- 协议 -->
        <el-form-item>
          <el-checkbox label="我已阅读并同意「用户协议」和「隐私条款」" v-model="ckecked"></el-checkbox>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '../../api/index.js'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'loginIndex',
  data () {
    const chackMobile = (rule, value, callback) => {
      if (/^1[3-9]\d{9}$/.test(value)) {
        callback()
      } else {
        callback(new Error('手机号格式错误'))
      }
    }
    return {
      loginForm: {
        mobile: '',
        code: ''
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
          { validator: chackMobile, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '验证码格式错误', trigger: 'blur' }
        ]
      },
      ckecked: true
    }
  },
  methods: {
    // 登录按钮方法
    submitForm () {
      this.$refs.loginForm.validate(async value => {
        if (value) {
          // 验证成功 调接口登录
          try {
            const { data: res } = await loginAPI(this.loginForm)
            console.log(res)
            // 将token存到本地
            setItem('geek-client-pc-store', res.data)
            // 跳转页面
            this.$router.push({ path: '/' })
          } catch (error) {
            // this.$message.error(error.response.message || '登录失败')
            if (error.response && error.response.status === 400) {
              this.$message.error('手机号或验证码错误')
            } else {
              this.$message.error('网络异常，请稍后再试')
              console.log(error)
            }
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url(../../assets/login.png);

  .el-card {
    width: 440px;
    height: 380px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.1);

    .logo {
      width: 200px;
      display: block;
      margin: 0 auto 20px;
    }

    .el-form {
      padding: 0 20px;

      .el-button {
        width: 100%;
      }
    }
  }

}
</style>
